package com.lancoo.klgcourseware.ui.fragment.operate;

import android.graphics.Typeface;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lancoo.klgcourseware.R;
import com.lancoo.klgcourseware.entity.bean.SpellCharacterBean;
import com.lancoo.klgcourseware.ui.adapter.SpellCharacterAdapter;
import com.lancoo.klgcourseware.utils.KlgToolUtils;
import com.lancoo.klgcourseware.utils.UniCodeUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pengmi on 2019/8/28 17:11
 * Description:
 */
public class AlertReadingTrainOperateFragment extends AlertBaseFragment {


    protected List<SpellCharacterBean> fastCharacterBeans,slowCharacterBeans;
    private boolean haiSlowTrain=true;
    private SpellCharacterAdapter mAdapter;

    @Override
    protected int getContentViewId() {
        return R.layout.klg_fragment_alert_phonetic_train;
    }

    @Override
    protected void initView(View view) {
        String klgName=getArguments().getString("klgName");
        String syllable=getArguments().getString("syllable");
        String phonetic=getArguments().getString("phonetic");
        splitSpellData(klgName,syllable);
        RecyclerView recyclerView = view.findViewById(R.id.alert_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false));
        if(haiSlowTrain){
            mAdapter = new SpellCharacterAdapter(getContext(),slowCharacterBeans);
        }else{
            mAdapter = new SpellCharacterAdapter(getContext(),fastCharacterBeans);
        }
        mAdapter.setTrainModel(true,false);
        mAdapter.setAlert(true);
        recyclerView.setAdapter(mAdapter);
        view.findViewById(R.id.tv_i_know).setOnClickListener(this);

        TextView tv_phonetic = view.findViewById(R.id.tv_phonetic);
        tv_phonetic.setText(UniCodeUtils.convertToChinese(phonetic));
        RelativeLayout rl_reading=view.findViewById(R.id.rl_spell_content_alert);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) rl_reading.getLayoutParams();
        int margTop= KlgToolUtils.dip2px(getContext(),KlgToolUtils.isPad(getContext())?246:202);
        layoutParams.topMargin=margTop+KlgToolUtils.getStatusHeight(getContext());
        try{
            Typeface typeFace = Typeface.createFromAsset(getContext().getAssets(), "klg_droid_sans_bold.ttf");
            tv_phonetic.setTypeface(typeFace);
        }catch (Exception e){

        }
    }

    /**
     * 单词拆分，作答总时长统计
     */
    private void splitSpellData( String klgName,String syllable){
        fastCharacterBeans=new ArrayList<>();
        slowCharacterBeans=new ArrayList<>();
        //拼写,快速环节数据组装
        for (int i = 0; i < klgName.length(); i++) {
            SpellCharacterBean spellCharacterBean=new SpellCharacterBean();
            spellCharacterBean.setCharacter(klgName.charAt(i)+"");
            fastCharacterBeans.add(spellCharacterBean);
        }

        //慢速环节数据组装
        String[] split =  syllable.split("·");
        int index=-1;
        for (int i = 0; i <split.length; i++) {
            int length=split[i].length();
            index+=length;
            SpellCharacterBean spellCharacterBean=new SpellCharacterBean();
            spellCharacterBean.setCharacter(split[i]);
            spellCharacterBean.setEndIndex(index);
            index+=1;
        }
        for (int i = 0; i < syllable.length(); i++) {
            SpellCharacterBean slowCharacterBean=new SpellCharacterBean();
            slowCharacterBean.setCharacter(syllable.substring(i,i+1));
            slowCharacterBeans.add(slowCharacterBean);
        }
        haiSlowTrain=split.length>1;
    }

    @Override
    public void onClick(View v) {
        if(System.currentTimeMillis()-time<500){
            return;
        }
        time=System.currentTimeMillis();
        super.onClick(v);
        int id=v.getId();
        if(id==R.id.tv_i_know){
            closeAlertOperate();
        }
    }
}
